package demo1;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:大根堆的向上调整
 * User: liangyuanzhang
 * Date: 2025-5-14
 * Time: 20:39
 */

/*
parent一直从地下往上走，到小于0结束
 */
public class Test {

    public void swap(int[] elem,int i,int j){
        int tmp = elem[i];
        elem[i] = elem[j];
        elem[j] = tmp;
    }

    public void push(int val){
        if (isFull()){
            elem = Arrays.copyOf(elem,2*elem.length);// 扩容
        }
        elem[usdeSize] = val;
        siftUp(usedSize);
        usedSize++;
    }

    public void siftUp(int child){
        int parent = (child-1)/2;
        while (parent >= 0){
            if (elem[child] > elem[parent]){
                swap(elem,child,parent);
                child = parent;
                parent = (child-1)/2;
            }else{
                break;
            }
        }
    }



}
